namespace KinonekoSoftware.Studio.Pages.Galleries.Filters
{
    public abstract class GalleryFilter : ObservableObject, IGalleryFilter
    {
        private InstanceGalleryViewModel2 _parent;

        private int    _pageBy;
        private SortBy _sortBy;
        private string _searchBy;

        protected GalleryFilter()
        {
            SearchCommand = new ActionCommand(DoSearchCommand);
        }

        internal void SetParent(InstanceGalleryViewModel2 parent)
        {
            _parent = parent;

            if (_parent is null)
            {
                return;
            }

            _searchBy = parent.SearchBy;
            _pageBy   = parent.PageBy;
            _sortBy   = parent.SortBy;
        }

        public abstract List<InstanceCache> Filter(ICollection<InstanceCache> collection, string searchBy);


        private void DoSearchCommand()
        {
            _parent?.ReloadDataSources();
        }

        public abstract string Name { get; }
        
        public string SearchBy
        {
            get => _searchBy;
            set
            {
                SetValue(ref _searchBy, value);
                

                if (_parent is not null)
                {
                    _parent.SearchBy = value;
                    _parent.OnFilterChanged();
                }
            }
        }

        public SortBy SortBy
        {
            get => _sortBy;
            set
            {
                SetValue(ref _sortBy, value);

                if (_parent is not null)
                {
                    _parent.SortBy = value;
                    _parent.OnFilterChanged();
                }
            }
        }

        public int PageBy
        {
            get => _pageBy;
            set
            {
                SetValue(ref _pageBy, value);

                if (_parent is not null)
                {
                    _parent.PageBy = value;
                    _parent.OnFilterChanged();
                }
            }
        }
        
        public ICommandEX SearchCommand { get; }
    }
}